package top.lenjon.lib.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lenjon.lib.entity.Admin;
import top.lenjon.lib.model.ResponseModel;
import top.lenjon.lib.service.IAdminService;
import top.lenjon.lib.service.IBookService;
import top.lenjon.lib.service.ILogService;
import top.lenjon.lib.service.IUserDetailService;

import java.util.Optional;

/**
 * @Author whz
 * @Date 2020/11/9 15:39
 **/
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    IAdminService iAdminService;
    @Autowired
    ILogService iLogService;
    @Autowired
    IBookService iBookService;
    @Autowired
    IUserDetailService iUserDetailService;

    /**
     * description:
     * 重置密码
     *
     * @param id
     * @param type
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/resetPassword")
    public ResponseModel resetPassword(Integer id, int type) {
        if (iAdminService.resetPassword(id, type)) {
            return ResponseModel.success("重置密码成功,000000");
        }
        return ResponseModel.error("重置密码失败");
    }

    /**
     * description:
     * 添加管理员
     *
     * @param admin
     * @return top.lenjon.lib.model.ResponseModel
     */
    @PostMapping("/addAdmin")
    public ResponseModel addAdmin(@RequestBody Admin admin) {
        admin.setType(1);
        admin.setEnabled(true);
        if (iAdminService.addAdmin(admin)) {
            return ResponseModel.success("添加管理员成功");
        }
        return ResponseModel.error("添加管理员失败");
    }


    /**
     * description:
     * 删除管理员
     *
     * @param id
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/deleteAdmin")
    public ResponseModel deleteAdmin(int id) {
        if (iAdminService.deleteAdmin(id)) {
            return ResponseModel.success("删除管理员成功");
        }
        return ResponseModel.error("删除管理员失败");
    }


    @GetMapping("/userList")
    public ResponseModel getUserList(@RequestParam(defaultValue = "1")int pageNum) {
        return Optional.ofNullable(iAdminService.getListByType(0,pageNum))
                .map(u -> ResponseModel.success("ok", u))
                .orElse(ResponseModel.error("list空"));
    }

    @GetMapping("/adminList")
    public ResponseModel getAdminList(@RequestParam(defaultValue = "1")int pageNum) {
        return Optional.ofNullable(iAdminService.getListByType(1,pageNum))
                .map(u -> ResponseModel.success("ok", u))
                .orElse(ResponseModel.error("list空"));
    }

    @GetMapping("/findUser")
    public ResponseModel findUser(String username){
        return Optional.ofNullable(iUserDetailService.getUserByUserName(username))
                .map(u -> ResponseModel.success("ok", u))
                .orElse(ResponseModel.error("无此人"));
    }

    @GetMapping("/findAdmin")
    public ResponseModel findAdmin(String username){
        return Optional.ofNullable(iAdminService.getAdminByUsername(username))
                .map(u -> ResponseModel.success("ok", u))
                .orElse(ResponseModel.error("无此人"));
    }


}
